package com.ecar.apm.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ecar.apm.dao.HttpRequestLogMapper;
import com.ecar.apm.dao.HttpRequestMapper;
import com.ecar.apm.http.client.HttpSequenceHandle;
import com.ecar.apm.model.HttpRequest;
import com.ecar.apm.model.HttpRequestLog;
import com.ecar.apm.model.HttpSequence;
import com.ecar.apm.quartz.DynamicJobManager;
import com.ecar.apm.service.HttpRequestService;
import com.ecar.apm.service.HttpSequenceService;
import com.ecar.apm.util.GuidGenerator;
import com.github.pagehelper.StringUtil;

@Service
public class HttpRequestServiceImpl implements HttpRequestService {

	 @Autowired
	  private HttpRequestMapper httpRequestMapper;
	  @Autowired
	  private HttpSequenceService httpSequenceService;
	  @Autowired
	  private HttpRequestLogMapper httpRequestLogMapper;
	  
	  @Transactional
	  public boolean enableMonitor(String guid)
	  {
	    HttpSequence instance = this.httpSequenceService.getByGuid(guid);
	    DynamicJobManager dynamicJobManager = new DynamicJobManager(instance);
	    return dynamicJobManager.enable();
	  }
	  
	  @Transactional
	  public boolean disableMonitor(String guid)
	  {
	    HttpSequence instance = this.httpSequenceService.getByGuid(guid);
	    DynamicJobManager dynamicJobManager = new DynamicJobManager(instance);
	    return dynamicJobManager.kill();
	  }
	  
	  @Transactional
	  public boolean deleteMonitor(String guid)
	  {
	    HttpSequence instance = this.httpSequenceService.getByGuid(guid);
	    DynamicJobManager dynamicJobManager = new DynamicJobManager(instance);
	    return dynamicJobManager.delete();
	  }
	  
	  @Transactional
	  public void executeRequest(String guid)
	  {
	    HttpSequence instance = this.httpSequenceService.getByGuid(guid);
	    if (instance == null) {
	      return;
	    }
	    List<HttpRequest> list = getHttpRequestListByPguid(guid);
	    instance.setHttpRequest(list);
	    
	    HttpSequenceHandle httpSequenceHandle = new HttpSequenceHandle(instance);
	    httpSequenceHandle.execute();
	    this.httpSequenceService.insertLog(httpSequenceHandle.httpSequenceLog);
	    for (HttpRequestLog httpRequestLog : httpSequenceHandle.httpRequestLogList)
	    {
	      httpRequestLog.setPid(httpSequenceHandle.httpSequenceLog.getId());
	      this.httpRequestLogMapper.insert(httpRequestLog);
	    }
	  }
	  
	  @Transactional
	  public void archivedHttpData(String guid)
	  {
	    this.httpSequenceService.archived(guid);
	    this.httpRequestMapper.archived(guid);
	  }
	  
	  @Transactional
	  public void deleteHttpLog(String guid)
	  {
	    this.httpSequenceService.deleteLog(guid);
	    this.httpRequestLogMapper.delete(guid);
	  }
	  
	  @Transactional
	  public void cleanMonitorLogs(int day)
	  {
	    this.httpRequestLogMapper.cleanLogByDay(day);
	    this.httpSequenceService.cleanLog(day);
	  }
	  
	  public void updateEnabled(HttpSequence httpSequence)
	  {
	    this.httpSequenceService.updateEnabled(httpSequence);
	  }
	  
	  public List<HttpRequest> getAllHttpRequest()
	  {
	    return this.httpRequestMapper.selectAll();
	  }
	  
	  public HttpRequest getHttpRequestByGuid(String guid)
	  {
	    return this.httpRequestMapper.getByGuid(guid);
	  }
	  
	  public HttpRequest getHttpRequestBypGuid(String pguid)
	  {
	    return this.httpRequestMapper.getBypGuid(pguid);
	  }
	  
	  public List<HttpRequest> getHttpRequestListByPguid(String pguid)
	  {
	    return this.httpRequestMapper.getListByPguid(pguid);
	  }
	  
	  public void insertHttpRequest(HttpRequest httpRequest)
	  {
	    if (StringUtil.isEmpty(httpRequest.getGuid())) {
	      httpRequest.setGuid(GuidGenerator.generate());
	    }
	    this.httpRequestMapper.insert(httpRequest);
	  }
	  
	  public void updateHttpRequest(HttpRequest httpRequest)
	  {
	    this.httpRequestMapper.update(httpRequest);
	  }
	  
	  public List<Map<String, Object>> getHttpRequestLogByPid(String id)
	  {
	    List<Map<String, Object>> list = this.httpRequestLogMapper.getByPid(Integer.parseInt(id));
	    return list;
	  }
}
